package cn.com.sp.demo.bean;

import java.io.IOException;
import java.util.Date;

import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

public class ThreadDemo extends Thread {

	private ResponseBodyEmitter responseBodyEmitter;
	private DeferredResult<String> deferredResult;

	public ThreadDemo() {

	}

	public ThreadDemo(ResponseBodyEmitter responseBodyEmitter) {
		this.responseBodyEmitter = responseBodyEmitter;
	}
	
	public ThreadDemo(DeferredResult<String> deferredResult) {
		this.deferredResult = deferredResult;
	}

	public void run() {

		try {
			System.out.println("start");
			Thread.sleep(5000);
			if(null != responseBodyEmitter)
			responseBodyEmitter.send(new Date());
			responseBodyEmitter.complete();
			if(null != deferredResult)
				deferredResult.setResult(new Date().toString());
			System.out.println("end");
		} catch (InterruptedException | IOException e) {
			System.out.println("exception");
			e.printStackTrace();
		}
	}
}
